package com.beijiteshop.shop.ui.address.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.beijiteshop.libcommon.base.adapter.showEmpty
import com.beijiteshop.libcommon.base.adapter.showError
import com.beijiteshop.libcommon.base.adapter.showLoading
import com.beijiteshop.libcommon.base.adapter.showNetError
import com.beijiteshop.libcommon.ext.databinding.bindView
import com.beijiteshop.libcommon.utils.EventUtil
import com.beijiteshop.shop.R
import com.beijiteshop.shop.app.PageIndex
import com.beijiteshop.shop.base.BaseContainerFragment
import com.beijiteshop.shop.databinding.FragmentAddressBinding
import com.beijiteshop.shop.model.event.EventKey
import com.beijiteshop.shop.ui.address.adapter.AddressAdapter
import com.beijiteshop.shop.ui.address.vm.AddressVM
import org.koin.android.viewmodel.ext.android.viewModel

/**
 * @Author LiaoZhongKai
 * @Date 2021/3/8 18:43
 * @Description 地址页面
 */
class AddressFragment : BaseContainerFragment(R.layout.fragment_address) {

    companion object{

        const val BUNDLE_IS_SELECT = "bundle_is_select"

        /**
         * 是否是选择地址，用以区别订单地址选择和我的--地址查看
         */
        fun newInstance(isSelect: Boolean): Fragment{
            return AddressFragment().apply {
                arguments = Bundle().apply {
                    putBoolean(BUNDLE_IS_SELECT,isSelect)
                }
            }
        }
    }

    private val mBinding by bindView<FragmentAddressBinding>()
    private val mAddressVM: AddressVM by viewModel()
    private val mAddressAdapter: AddressAdapter by lazy {
        AddressAdapter(mutableListOf())
    }
    private var mIsSelect = true

    override fun onLazyLoad() {
        requestData()
    }

    override fun initView(view: View) {
        mIsSelect = arguments?.getBoolean(BUNDLE_IS_SELECT)?:true
        initTitleBar()
        initRV()
    }

    override fun initData() {

    }

    override fun initEvent() {
        startObserve()
    }

    private fun initTitleBar(){
        mBinding.addressTitleBar.titleBarTitleTv.text = resources.getString(R.string.address)
        mBinding.addressTitleBar.titleBarBtnIv.setImageResource(R.drawable.ic_add)
        mBinding.addressTitleBar.titleBarBackIv.setOnClickListener {
            close()
        }
        mBinding.addressTitleBar.titleBarBtnIv.setOnClickListener {
            startActivity(PageIndex.ADD_ADDRESS, Bundle().apply {
                putInt(AddAddressFragment.BUNDLE_TYPE,AddAddressFragment.TYPE_ADD)
            })
        }
    }

    private fun initRV(){
        mAddressAdapter.apply {
            setOnItemChildClickListener { _, view, position ->
                val bundle = Bundle()
                val addressBean = data[position]
                bundle.putParcelable(AddAddressFragment.BUNDLE_ADDRESS,addressBean)
                bundle.putInt(AddAddressFragment.BUNDLE_TYPE,AddAddressFragment.TYPE_EDIT)
                startActivity(PageIndex.ADD_ADDRESS,bundle)
            }

            setOnItemClickListener { _, _, position ->
                if (mIsSelect){
                    EventUtil.post(EventKey.ORDER_ADDRESS,data[position])
                    close()
                }
            }
        }

        mBinding.addressRv.apply {
            layoutManager = LinearLayoutManager(requireContext())
            adapter = mAddressAdapter
        }
    }

    private fun requestData(){
        mAddressAdapter.showLoading()
        mAddressVM.getAddressList()
    }

    private fun startObserve(){
        mAddressVM.apply {
            liveAddressList.observeKt {addresses ->
                addresses?.let {
                    mAddressAdapter.setNewInstance(it.toMutableList())
                }
            }

            liveError.observeKt {
                mAddressAdapter.showError(it){
                    requestData()
                }
            }

            liveNetError.observeKt {
                mAddressAdapter.showNetError(it){
                    requestData()
                }
            }

            liveEmpty.observeKt {
                mAddressAdapter.setNewInstance(null)
                mAddressAdapter.showEmpty(it){
                    requestData()
                }
            }
        }

        //接收到地址添加成功事件
        EventUtil.observe<String?>(this,EventKey.UPDATE_ADDRESS){ msg ->
            if (!msg.isNullOrEmpty()){
                showToast(msg)
            }
            mAddressVM.getAddressList()
        }
    }

}